package com.my.vote.repository;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.my.vote.entity.VoteSubject;

public interface VoteSubjectRepository extends JpaRepository<VoteSubject, String> {

	@Query(value = " select s from VoteSubject s where s.delFlag = 0 order by s.createdTime desc")
	List<VoteSubject> findAllOrderCreateTime();

	@Query(value = " select s from VoteSubject s where s.delFlag = 0 and s.creatorId =?1 "
			+ " order by s.createdTime desc")
	List<VoteSubject> findAllByCreator(String creatorId);

	@Query(value = " select s from VoteSubject s where s.delFlag = 0 and s.title like '%?1%' "
			+ " order by s.createdTime desc")
	List<VoteSubject> findAllByTitle(String title);

	@Query(value = " select s from VoteSubject s where s.id in (select a.subjectId from VoteAnswer a where a.voterId = ?1) ")
	List<VoteSubject> findAllMyVoted(String voterId);

	@Transactional
	@Modifying(clearAutomatically = true)
	@Query(value = " update VoteSubject s set s.delFlag = 1 where s.delFlag = 0 and s.id = ?1 ")
	void deleteByid(String id);
}
